package com.sudichina.sudichina.querydatabase;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * author ywq on 2017/3/20.
 */
public class DBManager {
    private static final String ASSETS_NAME = "city.db";
    private static final String DB_NAME = "city.db";

    //城市表
    private static final String CITIES = "citys";


    private static final int BUFFER_SIZE = 1024;
    private static String DB_PATH;
    private static Context mContext;


    public DBManager(Context context) {
        this.mContext = context;
        DB_PATH = File.separator + "data"
                + Environment.getDataDirectory().getAbsolutePath() + File.separator
                + context.getPackageName() + File.separator + "databases" + File.separator;
    }

    @SuppressWarnings("ResultOfMethodCallIgnored")
    public void copyDBFile() {
        File dir = new File(DB_PATH);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File dbFile = new File(DB_PATH + DB_NAME);
        if (!dbFile.exists()) {
            InputStream is;
            OutputStream os;
            try {
                is = mContext.getResources().getAssets().open(ASSETS_NAME);
                os = new FileOutputStream(dbFile);
                byte[] buffer = new byte[BUFFER_SIZE];
                int length;
                while ((length = is.read(buffer, 0, buffer.length)) > 0) {
                    os.write(buffer, 0, length);
                }
                os.flush();
                os.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 查询数据库
     */


    /**
     * 根据城市id 查询 城市的对象 上级省的id
     */

    public Citys getCityFromCityName(String name) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);

        Cursor cursor = db.rawQuery("select * from " + CITIES + " WHERE ShortName =\"" + name + "\"", null);

        Citys Citys = null;
        while (cursor.moveToNext()) {

            String ID = cursor.getString(cursor.getColumnIndex("ID"));
            String ParentId = cursor.getString(cursor.getColumnIndex("ParentId"));

            String ShortName = cursor.getString(cursor.getColumnIndex("ShortName"));
            String MergerName = cursor.getString(cursor.getColumnIndex("MergerName"));
            String Ing = cursor.getString(cursor.getColumnIndex("lng"));//lng
            String Lat = cursor.getString(cursor.getColumnIndex("Lat"));
            String Pinyin = cursor.getString(cursor.getColumnIndex("Pinyin"));
            Citys = new Citys(ID, ParentId, ShortName, MergerName,Ing,Lat,Pinyin);

        }
        cursor.close();
        db.close();

        return Citys;
    }


    /**
     * 根据地区code查询对象
     */
    public  String  getCityFromCitycode(String code ) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null);

        Cursor cursor = db.rawQuery("select * from " + CITIES + " WHERE ID =\"" + code + "\"", null);

        Citys Citys = null; String ShortName=null;
        while (cursor.moveToNext()) {

            String ID = cursor.getString(cursor.getColumnIndex("ID"));
            String ParentId = cursor.getString(cursor.getColumnIndex("ParentId"));

              ShortName = cursor.getString(cursor.getColumnIndex("ShortName"));
            String MergerName = cursor.getString(cursor.getColumnIndex("MergerName"));
            String Ing = cursor.getString(cursor.getColumnIndex("lng"));//lng
            String Lat = cursor.getString(cursor.getColumnIndex("Lat"));
            String Pinyin = cursor.getString(cursor.getColumnIndex("Pinyin"));
            Citys = new Citys(ID, ParentId, ShortName, MergerName,Ing,Lat,Pinyin);

        }
        cursor.close();
        db.close();

        return ShortName;
    }
}
